/**
 * Created by WangMing on 15/12/9.
 */
define(["avalon",
    "text!./collectionWidget.html",
    "../../service",
    "../../utils/i18n",
    "pager"
], function(avalon, sourceHTML, api, i18n) {
    // 定义widget
    var widget = avalon.ui.collectionWidget = function(element, data, vmodels) {

        var vmodel = avalon.define(data.collectionWidgetId, function(vm) {


            // #Ajax 
            // params 
            //    type
            //    method // @TODO service.method:  api:getVideos
            //    params orders
            // has_pager
            // totalItems

            // #Array
            // results
            // collections 
            // total
            // pager


            vm.results = {};
            vm.isAjax = true;

            vm.collections = [];
            vm.size = 4;

            vm.params = {
                type: "", //"album",
                method: null, //api.getAlbum,
                query: {
                    queryString: [],
                    order: null
                }
            };

            vm.pager = {
                currentPage: vm.currentPage || 1,
                perPages: vm.perPages || 8,
                totalItems: vm.totalItems || 150,
                showJumper: false,
                alwaysShowNext: true, //@config {Boolean} 总是显示向后按钮
                alwaysShowPrev: true,
                onJump: function(e, data) {
                    // if backend handle pages
                    //location.href = "#page=" + data.currentPage
                    vm._getAndRenderCollections(vm.params, function(res) {
                        vm.pager.currentPage = data.currentPage;
                    });
                }

            }

            vm.perPages = 8;

            _.merge(vm, data.collectionWidgetOptions);
            vm.$skipArray = ["pager", "params", "results"]

            vm.$watch("perPages", function(a) {
                var widget = avalon.vmodels.pp
                if (widget) {
                    widget.perPages = a
                }
            })
            vm.haspager = function() {
                return vm.has_pager;
            }
            vm._renderList = function() {

            }
            vm._getAndRenderCollections = function(query, cb) {

                api.getItems(avalon.mix(vmodel.params, { query: query }), function(res) {
                    vm.results = res;

                    vm.collections = res.collections;

                    if (vm.collections && vm.collections.length) {
                        //stub  or use querystring.
                        if (vm.collections.length > vm.size) {
                            vm.collections = vm.collections.slice(0, vm.size);
                        }
                    }


                    vm.pager.totalItems = vm.results.total;


                    element.innerHTML = sourceHTML;

                    avalon.scan(element, [vmodel].concat(vmodels));

                    if (cb) {
                        cb(res);
                    }

                    setTimeout(function(){
                        $(window).trigger("imagesDefault");
                    },100)
                   


                })

            }

            vm.$init = function() {

                // page   totalitems  orders relatived(class category grade)
                if (vm.isAjax) {

                    vm.params.query.queryString.push("size=" + vm.size);

                    vm._getAndRenderCollections(vm.params);

                } else {
                    // input a array results. 
                    if (vm.size) {
                        vm.collections = vm.results.collections.slice(0, vm.size);
                    }


                    vm.pager.totalItems = vm.results.collections.total;

                    element.innerHTML = sourceHTML;

                    avalon.scan(element, [vmodel].concat(vmodels));

                    setTimeout(function(){
                        $(window).trigger("imagesDefault");
                    },100)
                }


            }




            vm.$remove = function() {
                element.innerHtml = element.textContent = "";
            }


        })

        return vmodel;
    }


    return avalon;

});
